package com.biblio.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.biblio.dom.Account;
import com.biblio.exceptions.AccountAlreadyExistsException;
import com.biblio.exceptions.AccountAlreadyExistsException.IdentifierType;
import com.biblio.repository.AccountRepository;
import com.biblio.utils.SecurityUtils;

@Repository
public class AccountService {
    @Autowired   private AccountRepository accountRepository;
    public Account registerAccount(Account account, String username, String password, String mail) throws AccountAlreadyExistsException {
    	
        if (accountRepository.getAccountByUserName(username) != null)    {
            throw new AccountAlreadyExistsException(IdentifierType.USERNAME, username);
        }
       
        if (accountRepository.getAccountByEmail(mail) != null){
            throw new AccountAlreadyExistsException(IdentifierType.MAIL, mail);
        }
        
        account.setPwd(SecurityUtils.md5Encode(password));


        return account;
    }
}
